home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / tk2.3 / dist / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-19  |  7.9 KB  |  323 lines

  1. /* 
  2.  * main.c --
  3.  *
  4.  *    A simple program to test the toolkit facilities.
  5.  *
  6.  * Copyright 1990-1992 Regents of the University of California.
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /user6/ouster/wish/RCS/main.c,v 1.68 92/05/07 08:52:02 ouster Exp $ SPRITE (Berkeley)";
  18. #endif
  19.  
  20. #include "tkConfig.h"
  21. #include "tkInt.h"
  22.  
  23. /*
  24.  * Declarations for library procedures:
  25.  */
  26.  
  27. extern int isatty();
  28.  
  29. /*
  30.  * Command used to initialize wish:
  31.  */
  32.  
  33. char initCmd[] = "source $tk_library/wish.tcl";
  34.  
  35. Tk_Window w;            /* NULL means window has been deleted. */
  36. Tk_TimerToken timeToken = 0;
  37. int idleHandler = 0;
  38. Tcl_Interp *interp;
  39. int x, y;
  40. Tcl_CmdBuf buffer;
  41. int tty;
  42. extern int Tk_SquareCmd _ANSI_ARGS_((ClientData clientData,
  43.     Tcl_Interp *interp, int argc, char **argv));
  44.  
  45. /*
  46.  * Information for testing out command-line options:
  47.  */
  48.  
  49. int synchronize = 0;
  50. char *fileName = NULL;
  51. char *name = NULL;
  52. char *display = NULL;
  53. char *geometry = NULL;
  54.  
  55. Tk_ArgvInfo argTable[] = {
  56.     {"-file", TK_ARGV_STRING, (char *) NULL, (char *) &fileName,
  57.     "File from which to read commands"},
  58.     {"-geometry", TK_ARGV_STRING, (char *) NULL, (char *) &geometry,
  59.     "Initial geometry for window"},
  60.     {"-display", TK_ARGV_STRING, (char *) NULL, (char *) &display,
  61.     "Display to use"},
  62.     {"-name", TK_ARGV_STRING, (char *) NULL, (char *) &name,
  63.     "Name to use for application"},
  64.     {"-sync", TK_ARGV_CONSTANT, (char *) 1, (char *) &synchronize,
  65.     "Use synchronous mode for display server"},
  66.     {(char *) NULL, TK_ARGV_END, (char *) NULL, (char *) NULL,
  67.     (char *) NULL}
  68. };
  69.  
  70.     /* ARGSUSED */
  71. void
  72. StdinProc(clientData, mask)
  73.     ClientData clientData;        /* Not used. */
  74.     int mask;
  75. {
  76.     char line[200];
  77.     static int gotPartial = 0;
  78.     char *cmd;
  79.     int result;
  80.  
  81.     if (mask & TK_READABLE) {
  82.     if (fgets(line, 200, stdin) == NULL) {
  83.         if (!gotPartial) {
  84.         if (tty) {
  85.             Tcl_Eval(interp, "destroy .", 0, (char **) NULL);
  86.             exit(0);
  87.         } else {
  88.             Tk_DeleteFileHandler(0);
  89.         }
  90.         return;
  91.         } else {
  92.         line[0] = 0;
  93.         }
  94.     }
  95.     cmd = Tcl_AssembleCmd(buffer, line);
  96.     if (cmd == NULL) {
  97.         gotPartial = 1;
  98.         return;
  99.     }
  100.     gotPartial = 0;
  101.     result = Tcl_RecordAndEval(interp, cmd, 0);
  102.     if (*interp->result != 0) {
  103.         if ((result != TCL_OK) || (tty)) {
  104.         printf("%s\n", interp->result);
  105.         }
  106.     }
  107.     if (tty) {
  108.         printf("wish: ");
  109.         fflush(stdout);
  110.     }
  111.     }
  112. }
  113.  
  114.     /* ARGSUSED */
  115. static void
  116. StructureProc(clientData, eventPtr)
  117.     ClientData clientData;    /* Information about window. */
  118.     XEvent *eventPtr;        /* Information about event. */
  119. {
  120.     if (eventPtr->type == DestroyNotify) {
  121.     w = NULL;
  122.     }
  123. }
  124.  
  125. /*
  126.  * Procedure to map initial window.  This is invoked as a do-when-idle
  127.  * handler.  Wait for all other when-idle handlers to be processed
  128.  * before mapping the window, so that the window's correct geometry
  129.  * has been determined.
  130.  */
  131.  
  132.     /* ARGSUSED */
  133. static void
  134. DelayedMap(clientData)
  135.     ClientData clientData;    /* Not used. */
  136. {
  137.  
  138.     while (Tk_DoOneEvent(TK_IDLE_EVENTS) != 0) {
  139.     /* Empty loop body. */
  140.     }
  141.     if (w == NULL) {
  142.     return;
  143.     }
  144.     Tk_MapWindow(w);
  145. }
  146.  
  147.     /* ARGSUSED */
  148. int
  149. DotCmd(dummy, interp, argc, argv)
  150.     ClientData dummy;            /* Not used. */
  151.     Tcl_Interp *interp;            /* Current interpreter. */
  152.     int argc;                /* Number of arguments. */
  153.     char **argv;            /* Argument strings. */
  154. {
  155.     int x, y;
  156.  
  157.     if (argc != 3) {
  158.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  159.         " x y\"", (char *) NULL);
  160.     return TCL_ERROR;
  161.     }
  162.     x = strtol(argv[1], (char **) NULL, 0);
  163.     y = strtol(argv[2], (char **) NULL, 0);
  164.     Tk_MakeWindowExist(w);
  165.     XDrawPoint(Tk_Display(w), Tk_WindowId(w),
  166.         DefaultGCOfScreen(Tk_Screen(w)), x, y);
  167.     return TCL_OK;
  168. }
  169.  
  170.     /* ARGSUSED */
  171. int
  172. MovetoCmd(dummy, interp, argc, argv)
  173.     ClientData dummy;            /* Not used. */
  174.     Tcl_Interp *interp;            /* Current interpreter. */
  175.     int argc;                /* Number of arguments. */
  176.     char **argv;            /* Argument strings. */
  177. {
  178.     if (argc != 3) {
  179.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  180.         " x y\"", (char *) NULL);
  181.     return TCL_ERROR;
  182.     }
  183.     x = strtol(argv[1], (char **) NULL, 0);
  184.     y = strtol(argv[2], (char **) NULL, 0);
  185.     return TCL_OK;
  186. }
  187.     /* ARGSUSED */
  188. int
  189. LinetoCmd(dummy, interp, argc, argv)
  190.     ClientData dummy;            /* Not used. */
  191.     Tcl_Interp *interp;            /* Current interpreter. */
  192.     int argc;                /* Number of arguments. */
  193.     char **argv;            /* Argument strings. */
  194. {
  195.     int newX, newY;
  196.  
  197.     if (argc != 3) {
  198.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  199.         " x y\"", (char *) NULL);
  200.     return TCL_ERROR;
  201.     }
  202.     newX = strtol(argv[1], (char **) NULL, 0);
  203.     newY = strtol(argv[2], (char **) NULL, 0);
  204.     Tk_MakeWindowExist(w);
  205.     XDrawLine(Tk_Display(w), Tk_WindowId(w),
  206.         DefaultGCOfScreen(Tk_Screen(w)), x, y, newX, newY);
  207.     x = newX;
  208.     y = newY;
  209.     return TCL_OK;
  210. }
  211.  
  212. int
  213. main(argc, argv)
  214.     int argc;
  215.     char **argv;
  216. {
  217.     char *args, *p, *msg;
  218.     char buf[20];
  219.     int result;
  220.     Tk_3DBorder border;
  221.  
  222.     interp = Tcl_CreateInterp();
  223. #ifdef TCL_MEM_DEBUG
  224.     Tcl_InitMemory(interp);
  225. #endif
  226.     if (Tk_ParseArgv(interp, (Tk_Window) NULL, &argc, argv, argTable, 0)
  227.         != TCL_OK) {
  228.     fprintf(stderr, "%s\n", interp->result);
  229.     exit(1);
  230.     }
  231.     if (name == NULL) {
  232.     if (fileName != NULL) {
  233.         p = fileName;
  234.     } else {
  235.         p = argv[0];
  236.     }
  237.     name = strrchr(p, '/');
  238.     if (name != NULL) {
  239.         name++;
  240.     } else {
  241.         name = p;
  242.     }
  243.     }
  244.     w = Tk_CreateMainWindow(interp, display, name);
  245.     if (w == NULL) {
  246.     fprintf(stderr, "%s\n", interp->result);
  247.     exit(1);
  248.     }
  249.     Tk_SetClass(w, "Tk");
  250.     Tk_CreateEventHandler(w, StructureNotifyMask, StructureProc,
  251.         (ClientData) NULL);
  252.     Tk_DoWhenIdle(DelayedMap, (ClientData) NULL);
  253.     tty = isatty(0);
  254.  
  255.     args = Tcl_Merge(argc-1, argv+1);
  256.     Tcl_SetVar(interp, "argv", args, TCL_GLOBAL_ONLY);
  257.     ckfree(args);
  258.     sprintf(buf, "%d", argc-1);
  259.     Tcl_SetVar(interp, "argc", buf, TCL_GLOBAL_ONLY);
  260.  
  261.     if (synchronize) {
  262.     XSynchronize(Tk_Display(w), True);
  263.     }
  264.     Tk_GeometryRequest(w, 200, 200);
  265.     border = Tk_Get3DBorder(interp, w, None, "#4eee94");
  266.     if (border == NULL) {
  267.     Tcl_SetResult(interp, (char *) NULL, TCL_STATIC);
  268.     Tk_SetWindowBackground(w, WhitePixelOfScreen(Tk_Screen(w)));
  269.     } else {
  270.     Tk_SetBackgroundFromBorder(w, border);
  271.     }
  272.     XSetForeground(Tk_Display(w), DefaultGCOfScreen(Tk_Screen(w)),
  273.         BlackPixelOfScreen(Tk_Screen(w)));
  274.     Tcl_CreateCommand(interp, "dot", DotCmd, (ClientData) w,
  275.         (void (*)()) NULL);
  276.     Tcl_CreateCommand(interp, "lineto", LinetoCmd, (ClientData) w,
  277.         (void (*)()) NULL);
  278.     Tcl_CreateCommand(interp, "moveto", MovetoCmd, (ClientData) w,
  279.         (void (*)()) NULL);
  280. #ifdef SQUARE_DEMO
  281.     Tcl_CreateCommand(interp, "square", Tk_SquareCmd, (ClientData) w,
  282.         (void (*)()) NULL);
  283. #endif
  284.     if (geometry != NULL) {
  285.     Tcl_SetVar(interp, "geometry", geometry, TCL_GLOBAL_ONLY);
  286.     }
  287.     result = Tcl_Eval(interp, initCmd, 0, (char **) NULL);
  288.     if (result != TCL_OK) {
  289.     goto error;
  290.     }
  291.     if (fileName != NULL) {
  292.     result = Tcl_VarEval(interp, "source ", fileName, (char *) NULL);
  293.     if (result != TCL_OK) {
  294.         goto error;
  295.     }
  296.     tty = 0;
  297.     } else {
  298.     tty = isatty(0);
  299.     Tk_CreateFileHandler(0, TK_READABLE, StdinProc, (ClientData) 0);
  300.     if (tty) {
  301.         printf("wish: ");
  302.     }
  303.     }
  304.     fflush(stdout);
  305.     buffer = Tcl_CreateCmdBuf();
  306.     (void) Tcl_Eval(interp, "update", 0, (char **) NULL);
  307.  
  308.     Tk_MainLoop();
  309.     Tcl_DeleteInterp(interp);
  310.     Tcl_DeleteCmdBuf(buffer);
  311.     exit(0);
  312.  
  313. error:
  314.     msg = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
  315.     if (msg == NULL) {
  316.     msg = interp->result;
  317.     }
  318.     fprintf(stderr, "%s\n", msg);
  319.     Tcl_Eval(interp, "destroy .", 0, (char **) NULL);
  320.     exit(1);
  321.     return 0;
  322. }
  323.